package com.sdgakj.rest;

import com.sdgakj.dto.FeedDTO;
import com.sdgakj.dto.base.ResCode;
import com.sdgakj.dto.base.ResResult;
import com.sdgakj.entity.Feed;
import com.sdgakj.enums.EnumFeedType;
import com.sdgakj.service.ServiceException;
import com.sdgakj.service.feed.FeedService;
import com.sdgakj.service.feed.FeedTagService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springside.modules.web.MediaTypes;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/api/v1/feedService")
public class FeedRestController {

	private static Logger logger = LoggerFactory.getLogger(FeedRestController.class);

	@Autowired
	private FeedService feedService;
	@Autowired
	private FeedTagService feedTagService;
	
	/**
	 * 根据时间倒叙获取帖子列表
	 * 
	 * @param userId
	 * @param tagId
	 * @param sinceId
	 * @param maxId
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "/get/list", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
	public ResResult listTimeline(@RequestParam(value = "userId", required = false) Long userId,
			@RequestParam(value = "tagId", required = false) Long tagId,
			@RequestParam(value = "sinceId", required = false) Long sinceId, 
			@RequestParam(value = "maxId", required = false) Long maxId,
			@RequestParam(value = "pageSize", required = false, defaultValue="10") Integer pageSize) {
		ResResult result = new ResResult();
		try {
			result.setData(feedService.getListTimeline(userId, tagId, sinceId, maxId, pageSize));
		} catch (ServiceException e) {
			result.setResCode(e.getResCode());
			logger.info("I-[帖子列表]业务错误", e);
		} catch (Exception e) {
			result.setResCode(ResCode.ERROR);
			logger.error("I-[帖子列表]程序异常", e);
		}
		return result;
	}
	
	/**
	 * 根据时间倒叙获取我的帖子列表
	 * 
	 * @param userId
	 * @param tagId
	 * @param sinceId
	 * @param maxId
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "/get/userList", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
	public ResResult myListTimeline(@RequestParam(value = "userId", required = true) Long userId,
			@RequestParam(value = "tagId", required = false) Long tagId,
			@RequestParam(value = "sinceId", required = false) Long sinceId, 
			@RequestParam(value = "maxId", required = false) Long maxId,
			@RequestParam(value = "pageSize", required = false, defaultValue="10") Integer pageSize) {
		ResResult result = new ResResult();
		try {
			result.setData(feedService.getMyListTimeline(userId, tagId, sinceId, maxId, pageSize));
		} catch (ServiceException e) {
			result.setResCode(e.getResCode());
			logger.info("I-[我的帖子列表]业务错误", e);
		} catch (Exception e) {
			result.setResCode(ResCode.ERROR);
			logger.error("I-[我的帖子列表]程序异常", e);
		}
		return result;
	}
	
	/**
	 * 根据时间正序机获取评论
	 * 
	 * @param userId
	 * @param id
	 * @param sinceId
	 * @param maxId
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "/comment/get/list", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
	public ResResult commentList(@RequestParam(value = "userId", required = false) Long userId,
			@RequestParam(value = "id", required = true) Long id,
			@RequestParam(value = "sinceId", required = false) Long sinceId, 
			@RequestParam(value = "maxId", required = false) Long maxId,
			@RequestParam(value = "pageSize", required = false, defaultValue="10") Integer pageSize) {
		ResResult result = new ResResult();
		try {
			result.setData(feedService.getCommentListTimeline(id, sinceId, maxId, pageSize));
		} catch (ServiceException e) {
			result.setResCode(e.getResCode());
			logger.info("I-[Feed评论列表]业务错误", e);
		} catch (Exception e) {
			result.setResCode(ResCode.ERROR);
			logger.error("I-[Feed评论列表]程序异常", e);
		}
		return result;
	}
	
	/**
	 * 获取feed详情
	 * 
	 * @param userId
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/get/one", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
	public ResResult one(@RequestParam(value = "userId", required = false) Long userId,
			@RequestParam(value = "id", required = true) Long id) {
		ResResult result = new ResResult();
		try {
			result.setData(feedService.getFeedById(id, userId));
		} catch (ServiceException e) {
			result.setResCode(e.getResCode());
			logger.info("I-[Feed详情]业务错误", e);
		} catch (Exception e) {
			result.setResCode(ResCode.ERROR);
			logger.error("I-[Feed详情]程序异常", e);
		}
		return result;
	}
	
	/**
	 * 创建帖子
	 * 
	 * @param userId
	 * @param tagId
	 * @param content
	 * @param pics
	 * @param cityName
	 * @param location
	 * @param latitude
	 * @param longitude
	 * @return
	 */
	@RequestMapping(value = "/create", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
	public ResResult create(@RequestParam(value = "userId", required = false) Long userId,
			@RequestParam(value = "tagId", required = true) Long tagId,
			@RequestParam(value = "content", required = true) String content,
			@RequestParam(value = "pics[]", required = false) String[]  pics,
			@RequestParam(value = "cityName", required = false) String cityName,
			@RequestParam(value = "location", required = false) String location,
			@RequestParam(value = "latitude", required = false) Double latitude,
			@RequestParam(value = "longitude", required = false) Double longitude) {
		ResResult result = new ResResult("发布成功");
		StringBuffer buffer=new StringBuffer();
		String pic=null;
		if(pics!=null) {
			for (int i = 0; i < pics.length; i++) {
				buffer.append(pics[i]);
				if (i != pics.length - 1) {
					buffer.append("|");
				}
			}
			pic = buffer.toString();
		}
		try {
			result.setData(feedService.createFeed(userId, tagId, content, pic, cityName, location, latitude, longitude));
		} catch (ServiceException e) {
			result.setResCode(e.getResCode());
			logger.info("I-[Feed创建]业务错误", e);
		} catch (Exception e) {
			result.setResCode(ResCode.ERROR);
			logger.error("I-[Feed创建]程序异常", e);
		}
		return result;
	}
	
	/**
	 * 创建评论
	 * 
	 * @param userId
	 * @param replyUserId
	 * @param content
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/comment/create", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
	public ResResult createComment(@RequestParam(value = "userId", required = false) Long userId,
			@RequestParam(value = "replyUserId", required = false,defaultValue = "1") Long replyUserId,
			@RequestParam(value = "content", required = true) String content,
			@RequestParam(value = "id", required = false,defaultValue = "1") Long id,
			@RequestParam(value = "tagId", required = false) Long tagId
								   ) {
		ResResult result = new ResResult("评论已发布");
		try {
			result.setData(feedService.createComment(userId, replyUserId, id, content));
		} catch (ServiceException e) {
			result.setResCode(e.getResCode());
			logger.info("I-[Feed创建评论]业务错误", e);
		} catch (Exception e) {
			result.setResCode(ResCode.ERROR);
			logger.error("I-[Feed创建评论]程序异常", e);
		}
		return result;

		/*return "h5/feed/message_note";*/
	}
	
	/**
	 * 赞
	 * 
	 * @param customerId
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/like/create", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
	public ResResult like(@RequestParam(value = "customerId", required = false) Long customerId,
			               @RequestParam(value = "id", required = true/*,defaultValue = "1"*/) Long id
						  /*@RequestParam(value = "likeCount", required = false) Long likeCount 后台已经从新获取了*/) {
		ResResult result = new ResResult();
		try {
			Long n=feedService.createLike(customerId,id);
			result.setData(n);
		} catch (ServiceException e) {
			result.setResCode(e.getResCode());
			logger.info("I-[Feed被赞]业务错误", e);
		} catch (Exception e) {
			result.setResCode(ResCode.ERROR);
			logger.error("I-[Feed被赞]程序异常", e);
		}
		return result;
	}
	
	/**
	 * 取消赞
	 * 
	 * @param customerId
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/like/delete", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
	public ResResult delLike(@RequestParam(value = "customerId", required = true) Long customerId,
			@RequestParam(value = "id", required = true) Long id) {
		ResResult result = new ResResult();
		try {
			feedService.delLike(customerId, id);
		} catch (ServiceException e) {
			result.setResCode(e.getResCode());
			logger.info("I-[Feed取消赞]业务错误", e);
		} catch (Exception e) {
			result.setResCode(ResCode.ERROR);
			logger.error("I-[Feed取消赞]程序异常", e);
		}
		return result;
	}
	
	/**
	 * 删除帖子
	 * 
	 * @param customerId
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
	public ResResult delFeed(@RequestParam(value = "customerId", required = true) Long customerId,
			@RequestParam(value = "id", required = true) Long id) {
		ResResult result = new ResResult("删除成功");
		try {
			feedService.delFeed(customerId, id);
		} catch (ServiceException e) {
			result.setResCode(e.getResCode());
			logger.info("I-[Feed删帖]业务错误", e);
		} catch (Exception e) {
			result.setResCode(ResCode.ERROR);
			logger.error("I-[Feed删帖]程序异常", e);
		}
		return result;
	}
	
	/**
	 * 删除评论
	 * 
	 * @param customerId
	 * @param commentId
	 * @return
	 */
	@RequestMapping(value = "/comment/delete", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
	public ResResult delComment(@RequestParam(value = "customerId", required = false) Long customerId,
			@RequestParam(value = "commentId", required = true) Long commentId) {
		ResResult result = new ResResult();
		try {
			System.out.println("commentId:"+commentId);
			feedService.delComment(customerId, commentId);
		} catch (ServiceException e) {
			result.setResCode(e.getResCode());
			logger.info("I-[Feed删评论]业务错误", e);
		} catch (Exception e) {
			result.setResCode(ResCode.ERROR);
			logger.error("I-[Feed删评论]程序异常", e);
		}
		return result;
	}
	
	/**
	 * 根据时间倒叙消息
	 * 
	 * @param userId
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "/msg/get/list", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
	public ResResult listTimeline(@RequestParam(value = "userId", required = true) Long userId,
			@RequestParam(value = "pageSize", required = false, defaultValue="30") Integer pageSize) {
		ResResult result = new ResResult();
		try {
			result.setData(feedService.getFeedMsgList(userId, pageSize));
		} catch (ServiceException e) {
			result.setResCode(e.getResCode());
			logger.info("I-[Feed消息列表]业务错误", e);
		} catch (Exception e) {
			result.setResCode(ResCode.ERROR);
			logger.error("I-[Feed消息列表]程序异常", e);
		}
		return result;
	}

	/*
	* 个人页面中增加历程 by yl
	* @param userId
	* @param processContent 内容
	* @param tagId
	* @param processTime 时间
	* @param latitude
	* @param longitude
	* @return
	* */
	@RequestMapping(value = "/personal/processadd", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
	public ResResult processadd(  @RequestParam(value = "userId", required = true) Long userId,
								   @RequestParam(value = "processContent", required = false) String content,
								   @RequestParam(value = "tagId",required = false) Long tagId,
								   @RequestParam(value = "processTime", required = true) Date createDate,
								   @RequestParam(value = "latitude", required = false) Double latitude,
								   @RequestParam(value = "longitude", required = false) Double longitude) {
		ResResult result = new ResResult("历程已经添加");
		try {
			result.setData(feedService.createFeed(userId, tagId, content, "", "", "", latitude, longitude ));
			//Long customerId, Long tagId, String content,
			//String pics, String cityName, String location, Double latitude, Double longitude
		} catch (ServiceException e) {
			result.setResCode(e.getResCode());
			logger.info("I-[Feed创建历程]业务错误", e);
		} catch (Exception e) {
			result.setResCode(ResCode.ERROR);
			logger.error("I-[Feed创建历程]程序异常", e);
		}
		return result;
	}
	/*
	* 个人页面中增加分享 by yl
	* @param userId
	* @param processContent 内容
	* @param tagId
	* @param processTime 时间
	* @param latitude
	* @param longitude
	* @return
	* */
	@RequestMapping(value = "/personal/pershare", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
	public ResResult pershare (  @RequestParam(value = "userId", required = true) Long userId,
								  @RequestParam(value = "processContent", required = false) String content,
								  @RequestParam(value = "tagId",required = false) Long tagId,
								  @RequestParam(value = "processTime", required = true) Date createDate,
								  @RequestParam(value = "latitude", required = false) Double latitude,
								  @RequestParam(value = "longitude", required = false) Double longitude) {
		ResResult result = new ResResult("分享已经添加");
		try {
			result.setData(feedService.createFeed(userId, tagId, content, "", "", "", latitude, longitude ));
			//Long customerId, Long tagId, String content,
			//String pics, String cityName, String location, Double latitude, Double longitude
		} catch (ServiceException e) {
			result.setResCode(e.getResCode());
			logger.info("I-[Feed创建分享]业务错误", e);
		} catch (Exception e) {
			result.setResCode(ResCode.ERROR);
			logger.error("I-[Feed创建分享]程序异常", e);
		}
		return result;
	}
	/*
        * 搜索 by yl
        * @param userId
        * @param processContent 内容
        * @param tagId
        * @param processTime 时间
        * @param latitude
        * @param longitude
        * @return
        * */
	@RequestMapping(value = "/feed/seacher", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
	public ResResult search(@RequestParam(value = "Id", required = false) Long Id,
						    @RequestParam(value = "tit", required = false) String tit,
						     Model model){
		ResResult result = new ResResult();
		try {
			List<Feed> list=feedService.getFeedlist(tit);
			result.setResCode(1);
			result.setData(list);
		}catch (Exception e){
			result.setResCode(0);
		}
		return result;
	}
}
